<%@ page import="com.example.dao.UserDao" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="com.example.util.DBUtil" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>用户管理 - 后台管理</title>
    <link rel="stylesheet" href="../assets/css/styles.css">
    <link rel="stylesheet" href="../assets/css/menu.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .page-title {
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #eee;
        }
        .btn {
            display: inline-block;
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 3px;
            border: none;
            cursor: pointer;
            margin-bottom: 20px;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .action-links a {
            margin-right: 10px;
            text-decoration: none;
            color: #007bff;
        }
        .action-links a:hover {
            text-decoration: underline;
        }
        .alert {
            padding: 12px;
            margin-bottom: 20px;
            border-radius: 3px;
        }
        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="page-title">用户管理</h1>
        
        <% if (request.getParameter("message") != null) { %>
            <div class="alert alert-success">
                <%= request.getParameter("message") %>
            </div>
        <% } %>
        
        <% if (request.getParameter("error") != null) { %>
            <div class="alert alert-error">
                <%= request.getParameter("error") %>
            </div>
        <% } %>
        
        <a href="user_add.jsp" class="btn">添加用户</a>
        
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>用户名</th>
                    <th>真实姓名</th>
                    <th>邮箱</th>
                    <th>电话</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <% 
                    try {
                        UserDao userDao = new UserDao();
                        List<User> users = userDao.getAllUsers();
                        for (User user : users) {
                %>
                <tr>
                    <td><%= user.getId() %></td>
                    <td><%= user.getUsername() %></td>
                    <td><%= user.getRealname() != null ? user.getRealname() : "" %></td>
                    <td><%= user.getEmail() %></td>
                    <td><%= user.getPhone() != null ? user.getPhone() : "" %></td>
                    <td class="action-links">
                        <a href="user_edit.jsp?id=<%= user.getId() %>">编辑</a>
                        <a href="javascript:void(0);" onclick="deleteUser(<%= user.getId() %>)">删除</a>
                    </td>
                </tr>
                <% 
                        }
                    } catch (Exception e) {
                %>
                <tr>
                    <td colspan="6" style="color: red;">加载用户列表失败: <%= e.getMessage() %></td>
                </tr>
                <% 
                    }
                %>
            </tbody>
        </table>
    </div>
    
    <script>
        function deleteUser(userId) {
            if (confirm('确定要删除这个用户吗？')) {
                // 创建表单并提交
                var form = document.createElement('form');
                form.method = 'POST';
                form.action = 'user_delete.jsp';
                
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = 'id';
                input.value = userId;
                
                form.appendChild(input);
                document.body.appendChild(form);
                form.submit();
            }
        }
    </script>
</body>
</html>